﻿using System;
using System.Windows;

namespace FloatingClock
{
	public partial class TimeSetWindow : Window
	{
		public DateTime SelectedTime { get; private set; }
		private TimeSpan? lastSetTime;

		public TimeSetWindow(TimeSpan? lastTime = null)
		{
			InitializeComponent();
			lastSetTime = lastTime;

			// 初始化当前时间
			datePicker.SelectedDate = DateTime.Now;
			timeTextBox.Text = DateTime.Now.ToString("HH:mm:ss");

			// 如果有上次设置的时间，启用按钮
			if (lastSetTime.HasValue)
			{
				lastTimeButton.IsEnabled = true;
				lastTimeButton.Content = $"使用上次设置的时间 ({lastSetTime.Value:hh\\:mm\\:ss})";
			}
		}

		private void LastTimeButton_Click(object sender, RoutedEventArgs e)
		{
			if (lastSetTime.HasValue)
			{
				timeTextBox.Text = lastSetTime.Value.ToString(@"hh\:mm\:ss");
			}
		}

		private void ConfirmButton_Click(object sender, RoutedEventArgs e)
		{
			if (datePicker.SelectedDate.HasValue && DateTime.TryParse($"{datePicker.SelectedDate.Value.ToString("yyyy-MM-dd")} {timeTextBox.Text}", out DateTime result))
			{
				SelectedTime = result;
				DialogResult = true;
			}
			else
			{
				MessageBox.Show("请输入有效的时间格式！");
			}
		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}
	}
}